package com.lucy.viewer.service;

import cn.hutool.core.lang.Validator;
import com.lucy.viewer.config.PreviewModeConfig;
import com.lucy.viewer.constant.ViewerServiceEnum;
import com.lucy.viewer.params.ViewerFile;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

/**
 * @author kindear
 * 预览服务
 */
@Slf4j
@Service
@AllArgsConstructor
public class PreviewService {
    List<ViewerService> services;

    PreviewModeConfig previewModeConfig;

    ViewerFileService viewerFileService;
    /**
     * 获取预览页面
     * @param documentKey
     * @return ModelAndView
     */
    public ModelAndView loadPreviewView(String documentKey, HttpServletResponse response) throws IOException{
        // 先查询本地,之后拓展查询接口
        ViewerFile viewerFile = viewerFileService.getViewerFile(documentKey);
        if (Validator.isEmpty(viewerFile)){
            throw new FileNotFoundException();
        }
        String fileType = viewerFile.getFileType();
        ViewerServiceEnum serviceEnum = ViewerServiceEnum.BROWSER;
        switch (fileType){
            case "doc":{
                serviceEnum = previewModeConfig.preview_doc();
                break;
            }
            case "docx":{
                serviceEnum = previewModeConfig.preview_docx();
                break;
            }
            case "docm":{
                serviceEnum = previewModeConfig.preview_docm();
                break;
            }
            case "dot":{
                serviceEnum = previewModeConfig.preview_dot();
                break;
            }
            case "rtf":{
                serviceEnum = previewModeConfig.preview_rtf();
                break;
            }
            case "xml":{
                serviceEnum = previewModeConfig.preview_xml();
                break;
            }
            case "xlsx":{
                serviceEnum = previewModeConfig.preview_xlsx();
                break;
            }
            case "xls":{
                serviceEnum = previewModeConfig.preview_xls();
                break;
            }
            case "csv":{
                serviceEnum = previewModeConfig.preview_csv();
                break;
            }
            case "pdf":{
                serviceEnum = previewModeConfig.preview_pdf();
                break;
            }
            case "txt":{
                serviceEnum = previewModeConfig.preview_txt();
                break;
            }
            case "jpg":{
                serviceEnum = previewModeConfig.preview_jpg();
                break;
            }
            case "md":{
                serviceEnum = previewModeConfig.preview_md();
                break;
            }
            case "java":{
                serviceEnum = previewModeConfig.preview_java();
                break;
            }
            case "css":{
                serviceEnum = previewModeConfig.preview_css();
                break;
            }
            case "c":{
                serviceEnum = previewModeConfig.preview_c();
                break;
            }
            case "h":{
                serviceEnum = previewModeConfig.preview_h();
                break;
            }
            case "m":{
                serviceEnum = previewModeConfig.preview_m();
                break;
            }
            case "py":{
                serviceEnum = previewModeConfig.preview_py();
                break;
            }
            case "cs":{
                serviceEnum = previewModeConfig.preview_cs();
                break;
            }
            case "cpp":{
                serviceEnum = previewModeConfig.preview_cpp();
                break;
            }
            case "php":{
                serviceEnum = previewModeConfig.preview_php();
                break;
            }
            case "R":{
                serviceEnum = previewModeConfig.preview_r();
                break;
            }
            case "json":{
                serviceEnum = previewModeConfig.preview_json();
                break;
            }
            case "js":{
                serviceEnum = previewModeConfig.preview_js();
                break;
            }
            case "sh":{
                serviceEnum = previewModeConfig.preview_sh();
                break;
            }
            case "html":{
                serviceEnum = previewModeConfig.preview_html();
                break;
            }
            case "vue":{
                serviceEnum = previewModeConfig.preview_vue();
                break;
            }
            default:{
                serviceEnum = previewModeConfig.preview_default();
            }
        }
        for (ViewerService service:services){
            if (serviceEnum.equals(service.service())){
                return service.loadPreviewView(documentKey,response);
            }
        }
        return null;

    }


}
